package top.lingkang.authoritypro.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.lingkang.authoritypro.data.dao.MenuDao;
import top.lingkang.authoritypro.data.entity.Menu;
import top.lingkang.authoritypro.data.vo.MenuVo;
import top.lingkang.authoritypro.service.MenuService;

import java.util.*;

/**
 * @author lingkang
 * @date 2021/12/19
 */
@Service
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;

    @Override
    public void addMenu(MenuVo menuVo) {
        Assert.isNull(menuDao.findByCode(menuVo.getCode()), "当前菜单编码已经存在！");
        Menu menu = new Menu();
        BeanUtil.copyProperties(menuVo, menu);
        menu.setStatus(1);
        menuDao.save(menu);
    }

    @Override
    public void editMenu(MenuVo menuVo) {
        Optional<Menu> byId = menuDao.findById(Long.parseLong(menuVo.getId().toString()));
        Assert.notNull(byId, "要修改的菜单Id不存在！");
        Assert.notNull(byId.get(), "要修改的菜单Id不存在！");
        Menu menu = new Menu();
        BeanUtil.copyProperties(menuVo, menu);
        menu.setId(byId.get().getId());
        menu.setParentCode(byId.get().getParentCode());
        menu.setStatus(byId.get().getStatus());
        menu.setCreateTime(byId.get().getCreateTime());
        menuDao.save(menu);
    }

    @Override
    public int deleteMenu(String code) {
        Set<String> menus = new HashSet<>();
        menus.add(code);
        List<Menu> childMenu = getChildMenu(code);
        if (CollUtil.isNotEmpty(childMenu)) {
            for (Menu menu : childMenu) {
                menus.add(menu.getCode());
            }
        }
        return menuDao.deleteAllByCodes(menus);
    }

    @Override
    public int batchDelete(List<String> codes) {
        Set<String> menus = new HashSet<>();
        menus.addAll(codes);
        for (String code : codes) {
            List<Menu> childMenu = getChildMenu(code);
            if (CollUtil.isNotEmpty(childMenu)) {
                for (Menu menu : childMenu) {
                    menus.add(menu.getCode());
                }
            }
        }
        return menuDao.deleteAllByCodes(menus);
    }

    /**
     * 递归获取所有子菜单
     */
    private List<Menu> getChildMenu(String code) {
        List<Menu> res = menuDao.findByParentCode(code);
        if (CollUtil.isNotEmpty(res)) {
            List<Menu> list = new ArrayList<>();
            for (Menu menu : res) {
                // 递归获取
                list = getChildMenu(menu.getCode());
            }
            res.addAll(list);
        }
        return res;
    }

    @Override
    public void addParentMneu(MenuVo menuVo) {
        Menu menu = new Menu();
        BeanUtil.copyProperties(menuVo, menu);
        menu.setId(null);
        menu.setCreateTime(null);
        menu.setUpdateTime(null);
        menu.setParentCode(null);
        menu.setStatus(1);
        menuDao.save(menu);
    }

    @Override
    public void editParentMneu(MenuVo menuVo) {
        Menu temp = menuDao.findByCode(menuVo.getCode());
        Assert.notNull(temp,"要编辑的菜单不存在！");
        Menu menu = new Menu();
        BeanUtil.copyProperties(menuVo, menu);
        menu.setId(temp.getId());
        menu.setCreateTime(temp.getCreateTime());
        menu.setUpdateTime(null);
        menu.setParentCode(null);
        menu.setStatus(temp.getStatus());
        menuDao.save(menu);
    }
}
